/**
 * 2010(c) Copyright Oceansoft Information System Co.,LTD. All rights reserved.
 * <p>
 * Compile: JDK 1.6+
 * <p>
 * 版权所有(C)：江苏欧索软件有限公司
 * <p>
 * 公司名称：江苏欧索软件有限公司
 * <p>
 * 公司地址：中国苏州科技城青山路1号
 * <p>
 * 网址: http://www.oceansoft.com.cn
 * <p>
 * 作者: 090922(陈伟)
 * <p>
 * 文件名: com.oceansoft.mobile.econsole.modules.sys.service.impl.ISysResourceServiceImpl.java 
 * <p>
 * 类产生时间: 2014-5-12 下午 16:22
 * <p>
 * 负责人: 090922(陈伟)
 * <p>
 * Email:javacspring@gmail.com
 * <p>
 * 所在组 : 掌上公安应用平台
 * <p>
 * 所在部门: 开发部--手持技术部
 * <p>
 * <p>
 */
package cn.com.oceansoft.business.sys.service.impl;

import cn.com.oceansoft.nos.common.constant.Constant;
import cn.com.oceansoft.nos.common.spring.SequenceGenerator;
import cn.com.oceansoft.nos.common.util.web.PageUtil;
import cn.com.oceansoft.business.base.entity.Pager;
import cn.com.oceansoft.business.base.entity.Query;
import cn.com.oceansoft.business.base.entity.Result;
import cn.com.oceansoft.business.sys.entity.RoleResource;
import cn.com.oceansoft.business.sys.entity.SysResource;
import cn.com.oceansoft.business.sys.persistence.ISysResourceMapper;
import cn.com.oceansoft.business.sys.service.ISysResourceService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.*;

/**
 * 资源管理业务组件接口实现类
 *
 * @author: chenw
 * @time: 2014-5-12 下午 16:22
 */
@Service
public class SysResourceServiceImpl implements ISysResourceService {

    @Resource
    private SequenceGenerator sequenceGenerator;

    @Resource
    private ISysResourceMapper sysResourceMapper;

    @Override
    public Pager<SysResource> findResourceByCondition(Query query) {
        PageHelper.startPage(query.getPageNum(), query.getNumPerPage(), true);
        Page<SysResource> page = (Page<SysResource>) sysResourceMapper.findPageByCondition(query);
        return PageUtil.page2pager(page, query);
    }

    @Override
    public SysResource findResourceIdById(int resourceId) {
        return sysResourceMapper.findResourceById(resourceId);
    }

    @Override
    public Result updateResource(SysResource resource) {
        int rt = sysResourceMapper.update(resource);
        if (rt == 1) {
            return new Result();
        } else {
            return new Result(Result.FAILURE, "系统资源信息更新失败！");
        }
    }

    @Override
    public Set<String> findResourceByUserId(String userId) {
        List<String> list = sysResourceMapper.findResourceByUserId(userId);
        return new HashSet<String>(list);
    }

    @Override
    public List<SysResource> findResourcesByRoleId(int roleId) {
        return sysResourceMapper.findResourceByRoleId(roleId);
    }

    @Override
    public List<SysResource> findResourcesTreeByRoleId(int roleId) {
        return sysResourceMapper.findResourcesTreeByRoleId(roleId);
    }

    @Override
    public Result updateRoleResources(int roleId, int resId, String operFlag) {
        if (StringUtils.hasText(operFlag)) {
            if (Constant.ACTION_READ.equals(operFlag)) {
                int rt = sysResourceMapper.saveRoleResources(roleId, resId);
            } else if (Constant.ACTION_DELETE.equals(operFlag)) {
                int rt = sysResourceMapper.deleteRoleResources(roleId, resId);
            } else {
                return new Result(Result.FAILURE, "参数错误");
            }
        } else {
            return new Result(Result.FAILURE, "参数错误");
        }
        return null;
    }

    @Override
    @Transactional
    public void updateRoleResourceBatch(final int roleId, final int[] resourceIds) {
        Map<String, Object> param = new HashMap<String, Object>() {{
            put("roleId", roleId);
            put("resIds", resourceIds);
        }};
        //删除指定ID的角色对应的资源信息
        sysResourceMapper.deleteRoleResourceRelationByRoleId(roleId);
        if (resourceIds.length > 0) {
            List<RoleResource> list = new ArrayList<RoleResource>(resourceIds.length);
            for (int resourceId : resourceIds) {
                list.add(new RoleResource(sequenceGenerator.nextRoleResourceIdSeq(), roleId, resourceId));
            }
            //批量插入关联信息
            sysResourceMapper.saveRoleResourceRelationByBatch(list);
        }
    }

    @Override
    @Transactional
    public Result remove(int id) {
        sysResourceMapper.deleteByKey(id);
        sysResourceMapper.deleteRoleResourceRelationByResId(id);
        return new Result();
    }

    @Override
    @Transactional
    public Result add(SysResource sysResource) {
        int row=sysResourceMapper.insert(sysResource);
        if (row > 0) {
            return new Result(sysResource);
        } else {
            return new Result(Result.FAILURE, "新增失败");
        }
    }

    @Override
    public List<SysResource> findResourcesByParentId(int id) {
        return sysResourceMapper.findResourceByParentId(id);
    }

    @Override
    public List<SysResource> findAll() {
        return sysResourceMapper.findAll();
    }
}





